<template>
	<view class="uni-container">
		<view class="uni-hello-text">
			<text class="hello-text">uni原生插件示例-POS机SDK</text>
		</view>
		<view class="uni-panel" v-for="(item, index) in list" :key="item.id">
			<view class="uni-panel-h" :class="item.open ? 'uni-panel-h-on' : ''" @click="triggerCollapse(index)">
				<text class="uni-panel-text">{{item.name}}</text>
				<text class="uni-panel-icon uni-icon" :class="item.open ? 'uni-panel-icon-on' : ''">{{item.pages ? '&#xe581;' : '&#xe470;'}}</text>
			</view>
			<view class="uni-panel-c" v-if="item.open">
				<view class="uni-navigate-item" v-for="(item2,key) in item.pages" :key="key" @click="goDetailPage(item2.url)">
					<text class="uni-navigate-text">{{item2.name ? item2.name : item2}}</text>
					<text class="uni-navigate-icon uni-icon">&#xe470;</text>
				</view>
			</view>
		</view>
	</view>
</template>
<script>
	
	// var main = plus.android.runtimeMainActivity();
	// var receiver;
	
	// import '@dcloudio/uni-amap'
	
	const modal = uni.requireNativePlugin('modal');
	
	var  main = plus.android.runtimeMainActivity(); //获取activity    
		 var context = plus.android.importClass('android.content.Context'); //上下文    
		var receiver;
		var IntentFilter = plus.android.importClass('android.content.IntentFilter');
		var Intent = plus.android.importClass('android.content.Intent');  
		var filter = new IntentFilter();  
	
	export default {
		data() {
			return {
				list: [
				{
					id: 'ext-module',
					name: 'Device Scan Sdk module',
					open: false,
					url: '/pages/sample/ext-module-device-scan'
				},
				{
					id: 'ext-module-rfid',
					name: 'RFID SDK module',
					open: false,
					url: '/pages/sample/ext-module-rfid'
				},
				{
					id: 'bluetooth-print',
					name: '便携式蓝牙打印机',
					open: false,
					url: '/pages/sample/bluetooth-print'
				},{
					id: 'bluetooth-sdk-print',
					name: '便携式蓝牙打印机(SDK)',
					open: false,
					url: '/pages/sample/bluetooth-sdk-print'
				},{
					id: 'ext-module-dt50-readid',
					name: 'DT50身份证硬解模块',
					open: false,
					url: '/pages/sample/ext-module-dt50-readid'
				},{
					id: 'ext-module-nfc-readid',
					name: 'NFC读身份证（联网）',
					open: false,
					url: '/pages/sample/ext-module-nfc-readid'
				},
				{
					id: 'ext-module-pos',
					name: 'POS SDK',
					open: false,
					url: '/pages/index/index-pos'
				}
				],
				navigateFlag: false
			}
		},
		// created() {
		// 			var _this=this;
		// 			 receiver = plus.android.implements('io.dcloud.feature.internal.reflect.BroadcastReceiver', {  
		// 				onReceive: function doReceive(context, intent) {  
		// 				//通过intent实例引入intent类，方便以后的‘.’操作    
		// 				//	plus.android.importClass(intent);  
		// 				//条码内容  
		// 				var barcodeBytes = intent.getByteArrayExtra("barcode_string");  
		// 				// var barcode_data = _this.byteToString(barcodeBytes);
		// 				console.info('======getData=============',intent.getStringExtra('barcode_string'))
		// 				// _this.lotId=barcode_data;
		// 				// _this.getData();
		// 				// 	console.info('======getData=============',intent.getStringExtra('barcode_string'))
		// 				}  
		// 			});  
		// 			//针对优博讯安卓PDA-i6300A添加监听，其它优博讯的型号应该一样或类似  
		// 			filter.addAction("android.intent.ACTION_DECODE_DATA"); //监听扫描  
		// 			main.registerReceiver(receiver, filter); //注册监听    
		// 			console.info('======registerReceiver=============')
		// 		},
		// 		destroyed() {
		// 			console.info('======destroyed=============')
		// 			main.unregisterReceiver(receiver);
		// 		},
		onLoad() {
		
			//        innerAudioContext.onPlay(() => {
			//          console.log('开始播放');
			//        });
			//        innerAudioContext.onError((res) => {
			//          console.log(res.errMsg);
			//          console.log(res.errCode);
			//        })';
			console.log('uni.getLocation()');
			uni.getLocation({
				// type: 'gcj02',
				type: 'wgs84',
				geocode: 'true',
				success: function (res) {
					console.log('经度：' + res.longitude);
					console.log('纬度：' + res.latitude);

					var imodal = this.modal;

						// 创建地图坐标对象
						var point = new plus.maps.Point(res.longitude, res.latitude);
						//静态方法，反向地理编码
						plus.maps.Map.reverseGeocode(point, {}, (event) => {
							var address = event.address; // 转换后的地理位置
							console.log('地址：' + JSON.stringify(event));
							
							modal.toast({
								message: '经度：' + event.coord.longitude+"\n"+'纬度：' + event.coord.latitude+"\n"+'地址：' + address,
								duration: 1.5
							});
							
							
						}, function(e) {
							console.log("失败回调", e);
						});



					
				}
			});

			
			
			plus.key.addEventListener("keydown", (e) => {
							console.log("keydown", e);
							if (e.keyCode == 520 || e.keyCode == 521 || e.keyCode == 522 || e.keyCode == 523) {
								console.log("扫描按键触发，keydown 键值为 " + e.keyCode);
								
							}
						});
			// plus.key.addEventListener("keyup", (e) => {
			// 				console.log("keyup", e);
			// 				if (e.keyCode == 520 || e.keyCode == 521 || e.keyCode == 522 || e.keyCode == 523) {
			// 					console.log("扫描按键触发，keyup 键值为 " + e.keyCode);
			// 					innerAudioContext.play();
			// 					// innerAudioContext.onError((res) => {
			// 					// 	console.log(res.errMsg);
			// 					// 	console.log(res.errCode);
			// 					// });
			// 				}
			// 			});
			
		},
		onShow() {
			// this.registerBroadcast();
			
			
			
			// var Intent = plus.android.importClass("android.content.Intent");  
			//     var intent = new Intent("com.android.broadcast.uscanner.settings");  
			//     var main = plus.android.runtimeMainActivity();  
			//     intent.putExtra("keyInt", -11);
			// 	intent.putExtra("valueInt", 1);  
			//     main.sendBroadcast(intent);
		},
		onHide() {
			// this.unRegisterBroadcast();
		},
		methods: {
			triggerCollapse(e) {
				if (!this.list[e].pages) {
					this.goDetailPage(this.list[e].url);
					return;
				}
				for (var i = 0; i < this.list.length; ++i) {
					if (e === i) {
						this.list[i].open = !this.list[e].open;
					} else {
						this.list[i].open = false;
					}
				}
			},
			goDetailPage(e) {
				if (this.navigateFlag) {
					return;
				}
				this.navigateFlag = true;
				uni.navigateTo({
					url: e
				});
				setTimeout(() => {
					this.navigateFlag = false;
				}, 200)
				return false;
			},
			
			
			registerBroadcast() {
				console.log('注册扫描广播')
				receiver = plus.android.implements('io.dcloud.feature.internal.reflect.BroadcastReceiver', {
					onReceive: doReceive
				});
				var that = this;
				var IntentFilter = plus.android.importClass('android.content.IntentFilter');
				var filter = new IntentFilter();
				filter.addAction("android.intent.ACTION_DECODE_DATA"); //监听扫描广播“urovo.rcv.message”  //android.intent.ACTION_DECODE_DATA
				main.registerReceiver(receiver, filter); //注册监听
				function doReceive(context, intent) {
					plus.android.importClass(intent);
					var barcodeStr = intent.getStringExtra("barcode_string");
					
					// var barcode = intent.getByteArrayExtra("barocode");
					// var barocodelen = intent.getIntExtra("length", 0);
					// var barcodeStr2 = String.fromCharCode.apply(null, barcode);
					// var barcodeStr = String.fromCharCode(...barcode);
					
					console.log("main-barcode:"+barcodeStr);
					modal.toast({
						message: barcodeStr,
						duration: 1.5
					});
					 that.textValue =  barcodeStr;
					// document.getElementsByClassName('text-box').innerText = barcodeStr;
				}
			},
			unRegisterBroadcast() {
				console.log('注销扫描广播')
				main.unregisterReceiver(receiver);
			}
			
			
		}
	}
</script>

<style>
</style>
